package com.zrrd.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author WanJl
 * @version 1.0
 * @title SpringRabbitListener
 * @description
 * @create 2024/7/19
 */
@Component
public class SpringRabbitListener {
    //指定消费者要接收哪一个队列里的消息
    /*@RabbitListener(queues = "simple.queue")
    public void receiveMessage(String message) {
        System.out.println(message);
    }*/


/*    @RabbitListener(queues = "fanout.queue02")
    public void receiveMessage(String message) {
        *//*try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
*//*
        System.out.println("消费者2从队列2种接收到消息----->"+message);
    }*/

    //1、利用@RabbitListener声明Exchange、Queue、RoutingKey
    //创建交换机wanjl.direct和队列direct.queue01，并且将他们绑定，同时让receiveMessage()进行监听
   /* @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name="direct.queue01"),
                    exchange = @Exchange(name = "wanjl.direct",type = ExchangeTypes.DIRECT),
                    key = {"aaa","zs"}
            )
    )
    public void receiveMessage(String message) {
        System.out.println("监听aaa和zs的消费者从队列1种接收到消息----->"+message);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name="direct.queue02"),
                    exchange = @Exchange(name = "wanjl.direct",type = ExchangeTypes.DIRECT),
                    key = {"aaa","lisi"}
            )
    )
    public void receiveMessage02(String message) {
        System.out.println("监听aaa和lisi的消费者从队列2种接收到消息----->"+message);
    }*/

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name="topic.queue01"),
                    exchange = @Exchange(name = "wanjl.topic",type = ExchangeTypes.TOPIC),
                    key = {"#.item"}
            )
    )
    public void receiveMessage(String message) {
        System.out.println("监听#.item的消费者从队列1种接收到消息----->"+message);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name="topic.queue02"),
                    exchange = @Exchange(name = "wanjl.topic",type = ExchangeTypes.TOPIC),
                    key = {"cart.*"}
            )
    )
    public void receiveMessage02(String message) {
        System.out.println("监听cart.#的消费者从队列2种接收到消息----->"+message);
    }


}
